﻿using CommonServiceLocator;
using GalaSoft.MvvmLight.CommandWpf;
using HuiZe.education.Common;
using HuiZe.education.Model;
using HuiZe.education.View;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using HuiZe.education.Resource.Controls;
using System.Linq;

namespace HuiZe.education.ViewModel
{
    //备课
    public class PlanLessonViewModel : ViewModelBase
    {
        public string BKTitle { get; set; } = "未选择备课章节，请从右侧课程资源选择备课章节";
        //public string BKName { get; set; }
        public ObservableCollection<ClassModel> Grades { get; set; } //班级
        public ObservableCollection<ClassModel> selectedClass2 { get; set; }
        public ObservableCollection<TreeData> classData { get; set; }
        public ClassModel selectedClass { get; set; }

        public int SeletedResCount { get; set; }//选中资源数
        public int ResourceViewSeletedResCount { get; set; }//选中资源数
        public string CurrentParentName { get; set; }//当前选中的分类
        public string CurrentCategoryName { get; set; }//当前选中的分类
        /// <summary>
        /// 正在显示选中的备课项
        /// </summary>
        public bool IsShowingSelectedBackageItem { get; set; }
        public int CurrentEditResDetailID { get; set; }
        public string ShowResDetailId { get; set; }//选中的待编辑的资源
        /// <summary>
        /// 是否正在新建
        /// </summary>
        public bool IsNewBuilding { get; set; }

        public List<resBKCategoryCourseChildModel> MyCollectResourceList0 { get; set; }
        public List<resBKCategoryCourseChildModel> MyCollectResourceList1 { get; set; }
        public List<resMyCollectMainModel> MyCollectResourceList { get; set; }
        public List<ReqGroupFileListDetialModel> MySpaceListdata { get; set; }
        public List<ReqGroupFileListDetialModel> MySpaceListdata1 { get; set; }
        public List<resBKCategoryCourseChildModel> CategoryCourseChildList { set; get; }
        public List<resBKCategoryCourseChildModel> CategoryCourseChildList1 { set; get; }
        public List<resCourseListChildModel> zjkjList { get; set; }
        public List<resCourseListChildModel> zjkjList1 { get; set; }

        public List<resCourseListChildModel> zjkjList2 { get; set; }
        public List<respResourceChildModel_Ethan> zjkjList3 { get; set; }
        public List<resCourseListChildModel> EthanMyPackageList { get; set; }

        public string attachment_ids { get; set; }

        public string ImageUrl { get; set; }
        public string MyImageUrl { get; set; }
        public string KeZyImageUrl { get; set; }

        public bool is_selected_one_reference { get; set; }

        public SelectChapterView selectChapterView { get; set; }
        public ResourceView resourceView { get; set; }

        public NextChapterView3 nextChapterView { get; set; }
        public List<resCourseListChildModel> CourseList { get; set; }
        public PlanLessonView planLessonView { get; set; }
        public List<resCourseTagChildModel> tagList { set; get; }
        public List<resCourseDirMainModel> DirList { get; set; }
        public string dir_name { set; get; }
        public string class_id { set; get; }
        public List<resCourseResourceMainModel> zjList { set; get; }
        private ObservableCollection<TreeModel> treenodes = new ObservableCollection<TreeModel>();
        private ObservableCollection<TreeModel> mytreenodes = new ObservableCollection<TreeModel>();
        public ObservableCollection<TreeModel> TreeNodes
        {
            get { return treenodes; }
            set
            {
                treenodes = value;
                //if (PropertyChanged != null)
                //    PropertyChanged(this, new PropertyChangedEventArgs("TreeNodes"));
            }
        }
        public ObservableCollection<TreeModel> myTreeNodes
        {
            get { return mytreenodes; }
            set
            {
                mytreenodes = value;
                Set("myTreeNodes", ref mytreenodes, value);
            }
        }

        private ObservableCollection<TreeModel> treenodes2 = new ObservableCollection<TreeModel>();
        public ObservableCollection<TreeModel> TreeNodes2
        {
            get { return treenodes2; }
            set
            {
                treenodes2 = value;
                Set("TreeNodes2", ref treenodes2, value);
            }
        }

        private List<string> ImagesPath { get; set; }
        public ObservableCollection<ResourceCenterModel> Resources { get; set; }
        //public string BkName { get; set; }

        private string _bk_name;
        public string BkName
        {
            get { return _bk_name; }
            set
            {
                _bk_name = value;
                Set("BkName", ref _bk_name, value);
            }
        }

        public RelayCommand<string> ResourceCenterCommand
        {
            get
            {
                return new RelayCommand<string>(new Action<string>((s) =>
                {
                    if (isUserAuth("2"))
                    {
                        //CategoryCourseChildList1.Clear();
                        //MyCollectResourceList1.Clear();
                        ResourceView resourceView = new ResourceView() { DataContext = this };
                        resourceView.Owner = System.Windows.Application.Current.MainWindow;
                        resourceView.ShowDialog();
                        if (resourceView.ShowDialogResult)
                        {

                        }
                    }
                }));
            }
        }

        /// <summary>
        /// 1 iscourse
        /// 2 resoure
        /// 3 yuncource
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool isUserAuth(string type)
        {
            userauthModel model = new userauthModel();

            model.token = HuiZeeducation.Instance.UserInfo.token;
            //model.file = filepath;


            string data2 = JsonHelper.ToJson(model);
            var result2 = Request<userauthRetModel>("/api/index/userauth", data2, "获取用户权限");
            if (result2.Item1)
            {
                if (type == "1")
                {
                    if (result2.Item2.data.is_course == "1")
                    {
                        return true;
                    }
                }
                else if (type == "2")
                {
                    if (result2.Item2.data.is_resource == "1")
                    {
                        return true;
                    }

                }
                else if (type == "3")
                {
                    if (result2.Item2.data.is_yuncource == "1")
                    {
                        return true;
                    }

                }
                else
                {
                    ShowDialogView("当前资源未授权，请联系管理员！");
                }
            }
            else
            {
                ShowDialogView(result2.Item2.msg);
            }
            ShowDialogView("当前资源未授权，请联系管理员！");
            return false;
        }

        ObservableCollection<ResourceCenterModel> _resources;
        public ObservableCollection<ResourceCenterModel> resources
        {
            get { return _resources; }
            set
            {
                _resources = value;
                Set("resources", ref _resources, value);
            }
        }

        public RelayCommand<string> SelectChapterCommand
        {
            get
            {
                return new RelayCommand<string>(new Action<string>((s) =>
                {
                    //DataContext = this;
                    //Window win = (Window)GetFrameworkElement(s);
                    //win.ShowDialog();
                    if (ResourceCount <= 0)
                    {
                        BkName = string.Empty;
                        CurrentParentName = string.Empty;
                        CurrentCategoryName = string.Empty;
                    }

                    if (string.IsNullOrEmpty(CurrentParentName))
                    {
                        SelectChapterView resourceView = new SelectChapterView() { DataContext = this };
                        resourceView.Owner = App.Current.MainWindow;
                        resourceView.ShowDialog();
                        if (resourceView.ShowDialogResult)
                        {

                        }
                    }
                    else
                    {
                        NextCommand.Execute(null);
                    }
                }));
            }
        }

        public RelayCommand MySpaceCommand
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {
                    MySpaceView mySpaceView = new MySpaceView() { DataContext = new MySpaceViewModel() };
                    if (mySpaceView.ShowDialog() == true)
                    {
                        //spaceFileList();
                        //Setattachment_ids(2, "");
                        //MySpaceListdata1.Clear();
                        //foreach (var item in MySpaceListdata)
                        //{
                        //    if (CommonMethods.MySpaceSelectedId.Contains(item.id.ToString()))
                        //    {
                        //        MySpaceListdata1.Add(item);
                        //    }
                        //}

                        var _list_selected = mySpaceView.SelectedItems;
                        if (_list_selected != null && _list_selected.Length > 0)
                        {
                            foreach (var _item in _list_selected)
                            {
                                if (!CommonMethods.MySpaceSelectedId.Contains(_item.id.ToString()))
                                {
                                    MySpaceListdata1.Add(_item);
                                    CommonMethods.MySpaceSelectedId.Add(_item.id.ToString());
                                }
                            }
                        }
                        planLessonView.UpdateImg3(MySpaceListdata1);
                    }

                }));
            }
        }
        public MySpaceViewModel MySpaceViewModel { get; set; }
        public SelectChapterViewModel SelectChapterView { get; set; }
        public ResourceCenterViewModel ResourceCenterView { get; set; }
        public RelayCommand UploadCommand { get; set; }
        public RelayCommand UploadSpaceCommand { get; set; }
        public int ResourceCount { get; set; }

        public ObservableCollection<BkModel> BkResList { get; set; }
        public BkModel BkModel { get; set; }
        //
        public PlanLessonViewModel()
        {
            var user = HuiZeeducation.Instance.UserInfo;
            GetBkClassList();
            zjkjList1 = new List<resCourseListChildModel>();
            EthanMyPackageList = new List<resCourseListChildModel>();
            MySpaceListdata1 = new List<ReqGroupFileListDetialModel>();
            CategoryCourseChildList1 = new List<resBKCategoryCourseChildModel>();
            MyCollectResourceList0 = new List<resBKCategoryCourseChildModel>();
            MyCollectResourceList1 = new List<resBKCategoryCourseChildModel>();
            if (IsInDesignMode)
            {
                ResourceCount = 9;
                MySpaceViewModel = new MySpaceViewModel();
                SelectChapterView = new SelectChapterViewModel();
                ResourceCenterView = new ResourceCenterViewModel();
            }
            ResourceCenterView = new ResourceCenterViewModel();
            InitCommand();



            classData = new ObservableCollection<TreeData>();
            if (HuiZeeducation.Instance.Classes != null)
            {
                foreach (var item in HuiZeeducation.Instance.Classes)
                {
                    TreeData data = new TreeData()
                    {
                        id = item.id,
                        DisplayName = item.name,
                        Children = new List<TreeData>()
                    };
                    foreach (var subClass in item.SubClass)
                    {
                        data.Children.Add(new TreeData()
                        {
                            id = subClass.id,
                            DisplayName = subClass.name
                        });
                    }
                    classData.Add(data);
                }
            }

            SelectClass2(classData.FirstOrDefault().Children.FirstOrDefault());
            selectedClass = Grades.FirstOrDefault(o => o.id == user.grades_id);
        }
        private void InitCommand()
        {
            UploadCommand = new RelayCommand(UpLoad);
            //UploadSpaceCommand = new RelayCommand(UpLoad2);
        }

        private void SelectChapter(string viewName)
        {
            PopupViewModel popupViewModel = ServiceLocator.Current.GetInstance<PopupViewModel>();
            popupViewModel.ShowPopupView(viewName);
        }

        public override void NavChanged(string viewName)
        {
            MainViewModel mainViewModel = ServiceLocator.Current.GetInstance<MainViewModel>();
            mainViewModel.NavChanged(viewName);
        }
        public void UpLoad()
        {
            if (MySpaceViewModel == null)
                MySpaceViewModel = new MySpaceViewModel();

            MySpaceViewModel.v_upload_mask = Visibility.Collapsed;
            UploadView uploadView = new UploadView();
            uploadView.Owner = System.Windows.Application.Current.MainWindow;
            uploadView.WaitingLoadData = true;
            uploadView.DataContext = MySpaceViewModel;
            if (uploadView.ShowDialog() == true)
            {
                var _list = MySpaceViewModel.spaceFileList(1);
                if (_list != null && _list.Count > 0)
                {
                    ReqGroupFileListDetialModel _current = null;
                    foreach (var _item in _list)
                    {
                        if (_item.id == MySpaceViewModel.current_upload_id)
                        {
                            _current = _item;
                            break;
                        }
                    }
                    if (_current != null)
                    {
                        var _list_id = new HashSet<int>();
                        var _list_select = new List<ReqGroupFileListDetialModel>();
                        foreach (var _item in EthanMyPackageList)
                        {
                            _list_id.Add(_item.id);
                        }
                        if (_list_id.Add(_current.id))
                        {
                            _list_select.Add(_current);
                            planLessonView.UpdateImg3(_list_select);
                        }
                    }
                }
            }

            //string filepath = "";
            //string filename = "";
            //string filesize = "";
            //OpenFileDialog dialog = new OpenFileDialog();
            //dialog.Multiselect = true;//该值确定是否可以选择多个文件
            //dialog.Title = "请选择文件";
            //dialog.Filter = "*|*";
            //if (dialog.ShowDialog() == DialogResult.OK)
            //{
            //    filepath = dialog.FileName;
            //    filename = dialog.SafeFileName;
            //    FileInfo fileInfo = new FileInfo(dialog.FileName);
            //    filesize = (fileInfo.Length / 1024).ToString();

            //}
            //if (string.IsNullOrWhiteSpace(filename))
            //{
            //    return;
            //}

            //ReqUploadModel model = new ReqUploadModel();

            //model.token = HuiZeeducation.Instance.UserInfo.token;
            ////model.file = filepath;


            //string data2 = JsonHelper.ToJson(model);
            //var result2 = Upload<ResUploadModel>("/api/common/oss_upload", data2, "上传文件", filepath);
            //string attachment_id = "";
            //if (result2.Item1)
            //{
            //    attachment_id = result2.Item2.data.attachment_id;
            //}
            //else
            //{
            //    ShowDialogView(result2.Item2.msg);
            //    return;
            //}

            //ReqGroupUserUploadModel2 pamrm = new ReqGroupUserUploadModel2
            //{
            //    token = HuiZeeducation.Instance.UserInfo.token,
            //    attachment_id = attachment_id,

            //};
            //string data = JsonHelper.ToJson(pamrm);
            //var result = Request<ReqGroupMainModel>("/api/user_space/userUploads", data, "上传备课包");
            //if (result.Item1)
            //{
            //    ShowDialogView("上传成功!");
            //}
            //else
            //{
            //    //ShowDialogView("上传成功!");
            //}
        }

        /// <summary>
        /// 获取授课班级列表
        /// </summary>
        public void GetBkClassList()
        {
            BkResList = new ObservableCollection<BkModel>();
            ReqBkClassModel model = new ReqBkClassModel();

            model.token = HuiZeeducation.Instance.UserInfo.token;

            string data = JsonHelper.ToJson(model);
            var result = Request<List<ResBkClassModel>>("/api/grades/classlist", data, "获取授课班级列表");
            if (result.Item1)
            {
                var bklist = result.Item2.data;
                foreach (var item in bklist)
                {

                    var bk2 = new BkModel
                    {
                        Id = item.class_id,
                        Name = item.grade_text,

                    };
                    //BkResList.Add(bk);
                    BkResList.Add(bk2);
                }
            }
            else
            {
            }

        }

        /// <summary>
        /// chapterId:章节id
        /// courseId:课程id
        /// </summary>
        /// <param name="chapterId"></param>
        /// <param name="courseId"></param>
        /// <returns></returns>
        public bool checkResource(int chapterId,int courseId)
        {
            CheckResourceModel model = new CheckResourceModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                //id = 2,
                kind_item_chapter_id = chapterId,
                id = courseId,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<ResBkClassModel>("/api/resources/checkResourcese", data, "编辑备课资源");
            if(result.Item2.code == 1)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 提交/编辑备课资源
        /// </summary>
        public bool EditClassResource()
        {
            if (string.IsNullOrWhiteSpace(BkName))
            {
                ShowDialogView("请输入备课标题!");
                return false;
            }
            string Attachment_ids = string.Empty;
            foreach (var _item in EthanMyPackageList)
            {
                Attachment_ids += $"t{_item.type_token}_{_item.id},";
            }
            //foreach (var item in zjkjList1)
            //{
            //    Attachment_ids += "t0" + "_" + item.id.ToString() + ",";
            //}

            //foreach (var item in CategoryCourseChildList1)
            //{
            //    Attachment_ids += "t1" + "_" + item.id.ToString() + ",";
            //}
            ////我的收藏
            //foreach (var item in MyCollectResourceList1)
            //{
            //    Attachment_ids += "t1" + "_" + item.id.ToString() + ",";
            //}
            //foreach (var item in MySpaceListdata1)
            //{
            //    Attachment_ids += "t2" + "_" + item.id.ToString() + ",";
            //}
            if (string.IsNullOrWhiteSpace(Attachment_ids))
            {

                ShowDialogView("请选择备课资源!");
                return false;
            }
            else
            {
                Attachment_ids = Attachment_ids.Substring(0, Attachment_ids.Length - 1);
            }

            //string class_ids = string.Empty;
            //foreach (var item in Grades)
            //{
            //    class_ids += item.id.ToString() + ",";
            //}
            //class_ids.Substring(0, class_ids.Length - 1);
            ReqEditBkClassModel model = new ReqEditBkClassModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                //class_ids = class_ids,
                title = BkName,
                id = CurrentEditResDetailID,
                attachment_ids = Attachment_ids,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<ResBkClassModel>("/api/resources/takeBclassInfo", data, "编辑备课资源");
            if (result.Item1)
            {
                ShowDialogView("保存备课资源成功!");
                return true;
            }
            else
            {
                ShowDialogView(result.Item2.msg);
                return false;
            }
        }
        /// <summary>
        /// 备课资源详情
        /// </summary>
        public void GetClassResourceDetail()
        {

            ReqBkClassDetailModel model = new ReqBkClassDetailModel();

            model.token = HuiZeeducation.Instance.UserInfo.token;

            string data = JsonHelper.ToJson(model);
            var result = Request<List<ResBkClassDetailModel>>("/api/resources/beikelist", data, "备课资源详情");
            if (result.Item1)
            {
                ShowDialogView("删除成功!");
            }
            else
            {
            }

        }

        /// <summary>
        /// 我的备课列表/回收站
        /// </summary>
        public void GetMyBkResourceList()
        {

            ReqMyBkResourceModel model = new ReqMyBkResourceModel();

            model.token = HuiZeeducation.Instance.UserInfo.token;

            string data = JsonHelper.ToJson(model);
            var result = Request<ResMyBkResourceModel>("/api/resources/myBclassList", data, "我的备课列表");
            if (result.Item1)
            {
                //ShowDialogView("删除成功!");
            }
            else
            {

            }

        }

        /// <summary>
        /// 置顶资源
        /// </summary>
        public void SetBkResourceWeigh()
        {
            ReqUpResourceModel model = new ReqUpResourceModel();
            model.token = HuiZeeducation.Instance.UserInfo.token;
            string data = JsonHelper.ToJson(model);
            var result = Request<ResUpResourceModel>("/api/resources/setWeigh", data, "置顶资源");
            if (result.Item1)
            {
                ShowDialogView("置顶成功!");
            }
            else
            {
            }

        }

        /// <summary>
        /// 开始上课资源列表
        /// </summary>
        public void GetbeikeList()
        {

            ReqBkClassDetailModel model = new ReqBkClassDetailModel();

            model.token = HuiZeeducation.Instance.UserInfo.token;

            string data = JsonHelper.ToJson(model);
            var result = Request<List<ResStartResourceModel>>("/api/resources/beikelist", data, "开始上课资源列表");
            if (result.Item1)
            {
                //ShowDialogView("删除成功!");
            }
            else
            {
            }

        }

        /// <summary>
        /// 删除备课包
        /// </summary>
        public void DelBclass()
        {

            ReqUpResourceModel model = new ReqUpResourceModel();

            model.token = HuiZeeducation.Instance.UserInfo.token;

            string data = JsonHelper.ToJson(model);
            var result = Request<ResDelBkModel>("/api/resources/delBclass", data, "删除备课包");
            if (result.Item1)
            {
                ShowDialogView("删除成功!");
            }
            else
            {
            }

        }



        /// <summary>
        /// 共享资源包/我的共享资源列表
        /// </summary>
        public void getShareList()
        {

            ReqShareListModel model = new ReqShareListModel();

            model.token = HuiZeeducation.Instance.UserInfo.token;

            string data = JsonHelper.ToJson(model);
            var result = Request<ResShareResourceListModel>("/api/resources/getShareList", data, "共享资源包");
            if (result.Item1)
            {
                //ShowDialogView("删除成功!");
            }
            else
            {

            }
        }

        /// <summary>
        /// 添加共享/修改共享
        /// </summary>
        public void SetResourcesShare()
        {

            ReqAddShareModel model = new ReqAddShareModel();

            model.token = HuiZeeducation.Instance.UserInfo.token;

            string data = JsonHelper.ToJson(model);
            var result = Request<ResDelBkModel>("/api/resources/share", data, "添加共享");
            if (result.Item1)
            {
                ShowDialogView("共享成功!");
            }
            else
            {

            }
        }

        /// <summary>
        /// 查看备课包资源
        /// </summary>
        public void GetBcalssInfo()
        {

            ReqUpResourceModel model = new ReqUpResourceModel();

            model.token = HuiZeeducation.Instance.UserInfo.token;

            string data = JsonHelper.ToJson(model);
            var result = Request<ResBcalssModel>("/api/resources/getBcalssInfoList", data, "查看备课包资源");
            if (result.Item1)
            {
                //ShowDialogView("删除成功!");
            }
            else
            {

            }
        }

        public void SetImagesPath(string path)
        {
            if (ImagesPath == null) ImagesPath = new List<string>();
            ImagesPath.Add(path);
        }

        public void GetSourceList_Ethan()
        {
            resources = new ObservableCollection<ResourceCenterModel>();

            try
            {
                ReqresourceCenterModel model = new ReqresourceCenterModel();
                model.token = HuiZeeducation.Instance.UserInfo.token;
                string data = JsonHelper.ToJson(model);
                var result = Request<List<respResourceChildModel>>("/api/ziyuan/getKindList", data, "获取资源列表");
                if (result.Item1)
                {
                    foreach (var _item in result.Item2.data)
                    {
                        var _center = new ResourceCenterModel
                        {
                            id = _item.id,
                            kind_name = _item.kind_name,
                            resources = new ObservableCollection<respResourcekinditemModel>(),
                        };
                        //var _list_kind = GetKindItemList(_item.id);
                        //if (_list_kind != null)
                        //    _center.resources = new ObservableCollection<respResourcekinditemModel>(_list_kind);
                        resources.Add(_center);
                    }
                }
            }
            catch (Exception ex)
            {
                App.Log(ex);
            }
        }

        public void GetSourceList_Ethan(ResourceCenterModel _center_model)
        {
            if (_center_model == null) return;

            if (_center_model.resources == null)
                _center_model.resources = new ObservableCollection<respResourcekinditemModel>();

            if (_center_model.resources.Count > 0) return;

            var _list_kind = GetKindItemList(_center_model.id);
            if (_list_kind != null)
                _center_model.resources = new ObservableCollection<respResourcekinditemModel>(_list_kind);
        }

        public void GetSourceList_Ethan(resMyCollectMainModel _center_model)
        {
            if (_center_model == null) return;

            if (_center_model.resources == null)
                _center_model.resources = new ObservableCollection<resKindItemModel>();

            if (_center_model.resources.Count > 0) return;

            var _list_kind = GetCategoryCourseList(_center_model.c_id);
            if (_list_kind != null)
                _center_model.resources = new ObservableCollection<resKindItemModel>(_list_kind);
        }

        public List<resKindItemModel> GetCategoryCourseList(int id)
        {
            ReqresourceDetailModel model = new ReqresourceDetailModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = id,
                limit = 200,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<respResourceChildModel>("/api/ziyuan/getZiyuanList", data, "获取资源列表");
            if (result.Item1)
            {
                return result.Item2.data.data.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }
        }

        public respResourceChildModel_Ethan[] GetCategoryCourseList_Ethan(int id)
        {
            ReqresourceDetailModel model = new ReqresourceDetailModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = id,
                limit = 200,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<respResourceChildModel_Ethan[]>("/api/ziyuan/getZiyuanList", data, "获取资源列表");
            if (result.Item1)
            {
                return result.Item2.data;
                //return result.Item2.data.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }
        }

        public List<resMyCollectMainModel> GetMySourceListList_Ethan()
        {
            ReqreMysourceModel model = new ReqreMysourceModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<resMyCollectTopModel>("/api/ziyuan/getMyZiyuanList", data, "获取资源列表");
            if (result.Item1)
            {
                MyCollectResourceList = result.Item2.data.data;
                if (MyCollectResourceList != null)
                {
                    foreach (var _item in MyCollectResourceList)
                    {
                        _item.resources = new ObservableCollection<resKindItemModel>();
                    }
                }
                return MyCollectResourceList;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }
        }

        public void GetSourceList()
        {

            Resources = new ObservableCollection<ResourceCenterModel>();

            ReqresourceCenterModel model = new ReqresourceCenterModel();
            model.token = HuiZeeducation.Instance.UserInfo.token;
            string data = JsonHelper.ToJson(model);
            var result = Request<List<respResourceChildModel>>("/api/ziyuan/getKindList", data, "获取资源列表");
            if (result.Item1)
            {

                treenodes.Clear();
                var resources = result.Item2.data;

                foreach (var res in resources)
                {
                    TreeModel treemodel = new TreeModel
                    {
                        NodeID = res.id,
                        NodeName = res.kind_name,

                    };
                    List<TreeModel> TreeList = new List<TreeModel>();
                    var kindList = GetKindItemList(res.id);
                    if (kindList != null)
                    {
                        foreach (var item in kindList)
                        {
                            TreeModel childtree = new TreeModel
                            {
                                NodeID = item.id,
                                NodeName = item.kind_item_name,
                                ParentID = res.id,
                            };
                            TreeList.Add(childtree);
                        }
                    }
                    treemodel.ChildNodes = new ObservableCollection<TreeModel>(TreeList);
                    treenodes.Add(treemodel);

                    //var resource = new ResourceCenterModel
                    //{
                    //    kind_name = res.kind_name,
                    //    id = res.id
                    //};
                    //Resources.Add(resource);
                }
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
            }
        }

        public List<respResourcekinditemModel> GetKindItemList(int Id)
        {
            Resources = new ObservableCollection<ResourceCenterModel>();
            ReqresourceCenterModel2 model = new ReqresourceCenterModel2()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = Id,
                limit = 200,
                p = 1,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<respResourcekinditemMainModel>("/api/ziyuan/getKind_itemList", data, "获取资源子列表");
            if (result.Item1)
            {
                //treenodes.Clear();
                return result.Item2.data.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }
        }

        public List<resBKCategoryCourseChildModel> GetMyCollectionByName(string name)
        {
            int kindid = 0;
            string kindname = "";
            foreach (var item in MyCollectResourceList)
            {

                if (item.ziyuan.kind_item_name == name)
                {
                    kindid = item.ziyuan.kind_item_id;
                    kindname = item.ziyuan.kind_item_name;
                }
            }

            var list = GetCategoryCourseList(kindid, kindname);
            if (list != null)
                return list;
            else
                return null;

        }

        public List<resBKCategoryCourseChildModel> GetCategoryCourseList(int id, string type)
        {
            ReqresourceDetailModel model = new ReqresourceDetailModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = id,
                filetype = "全部"
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<resBKCategoryCourseMainModel>("/api/ziyuan/getZiyuanList", data, "获取资源列表");
            if (result.Item1)
            {
                CategoryCourseChildList = result.Item2.data.data.data;
                return result.Item2.data.data.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }
        }

        public List<resMyCollectMainModel> GetMySourceListList(string type)
        {

            ReqreMysourceModel model = new ReqreMysourceModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,

                //filetype = type

            };


            string data = JsonHelper.ToJson(model);
            var result = Request<resMyCollectTopModel>("/api/ziyuan/getMyZiyuanList", data, "获取资源列表");
            if (result.Item1)
            {
                mytreenodes.Clear();
                foreach (var item in result.Item2.data.data)
                {
                    TreeModel treemodel = new TreeModel
                    {
                        NodeID = item.ziyuan.kind_item_id,
                        NodeName = item.ziyuan.kind_item_name,

                    };
                    List<TreeModel> TreeList = new List<TreeModel>();

                    //var kindList = GetCategoryCourseList(item.ziyuan.kind_item_id, item.ziyuan.kind_item_name);
                    //if (kindList != null)
                    //{
                    //    foreach (var item2 in kindList)
                    //    {
                    //        TreeModel childtree = new TreeModel
                    //        {
                    //            NodeID = item2.id,
                    //            NodeName = item2.resources_name,
                    //            ParentID = item.ziyuan.kind_item_id,
                    //        };
                    //        TreeList.Add(childtree);

                    //    }
                    //}
                    //treemodel.ChildNodes = TreeList;



                    mytreenodes.Add(treemodel);
                    //var list = GetCategoryCourseList(item.ziyuan.kind_item_id, item.ziyuan.kind_item_name);
                }

                MyCollectResourceList = result.Item2.data.data;
                return result.Item2.data.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }

        }

        public List<resCourseTagChildModel> taglist { get; set; }
        public List<resCourseTagChildModel> GetTagsList()
        {

            ReqCourseSourceModel model = new ReqCourseSourceModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
            };


            string data = JsonHelper.ToJson(model);
            var result = Request<List<resCourseTagChildModel>>("/api/resources/getTagsList", data, "获取资源列表");
            if (result.Item1)
            {
                taglist = result.Item2.data;
                return result.Item2.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }

        }

        public List<resCourseListChildModel> GetResourcesCourseList(string tagName)
        {
            int id = 0;
            int tagid = 0;
            foreach (var item in taglist)
            {
                if (item.tag_name == tagName)
                {
                    id = item.id;
                    tagid = item.tag_id;

                }
            }

            ReqCourseSourceListModel model = new ReqCourseSourceListModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = id,
                tags = tagid,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<List<resCourseListChildModel>>("/api/resources/getResourcesList", data, "获取资源列表");
            if (result.Item1)
            {
                CourseList = result.Item2.data;

                return result.Item2.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);

                return null;
            }

        }

        /// <summary>
        /// 获取章节类表
        /// </summary>
        public void GetzjSourceList()
        {
            ReqCourseSourceModel model = new ReqCourseSourceModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<List<resCourseResourceMainModel>>("/api/resources/getKindList", data, "获取资源列表");
            if (result.Item1)
            {
                TreeNodes2 = new ObservableCollection<TreeModel>();
                //treenodes2.Clear();
                var resources = result.Item2.data;
                zjList = resources;
                foreach (var res in resources)
                {
                    TreeModel treemodel = new TreeModel
                    {
                        NodeID = res.id,
                        NodeName = res.kind_name,
                        ChildNodes = new ObservableCollection<TreeModel>(),
                    };
                    foreach (var item in res.kind_item)
                    {
                        TreeModel childtree = new TreeModel
                        {
                            NodeID = item.id,
                            NodeName = item.kind_item_name,
                            ParentID = res.id,
                        };
                        treemodel.ChildNodes.Add(childtree);
                    }
                    TreeNodes2.Add(treemodel);//treenodes2.Add(treemodel);
                }
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
            }
        }

        /// <summary>
        /// 获取章节列表
        /// </summary>
        public List<resCourseDirMainModel> GetzjDirList(int id)
        {
            int kind_item_id = 0;
            foreach (var res in zjList)
            {
                foreach (var item in res.kind_item)
                {
                    if (item.id == id)
                        kind_item_id = item.kind_item_id;
                }
            }
            if (kind_item_id == 0)
                return null;

            ReqCourseDirModel2 model = new ReqCourseDirModel2()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                //item_id = kind_item_id.ToString()
                kind_item_id = kind_item_id.ToString(),
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<List<resCourseDirMainModel>>("/api/resources/getDirList", data, "获取资源列表");
            if (result.Item1)
            {
                DirList = result.Item2.data;
                return result.Item2.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }

        }
        /// <summary>
        /// 获取章节标签
        /// </summary>
        public List<resCourseTagChildModel> GetzjTagsList()
        {
            if (string.IsNullOrWhiteSpace(class_id))
                return null;

            ReqCourseSourceModel model = new ReqCourseSourceModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                class_id = class_id
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<List<resCourseTagChildModel>>("/api/resources/getTagsList", data, "获取资源列表");
            if (result.Item1)
            {
                tagList = result.Item2.data;
                return result.Item2.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }

        }

        /// <summary>
        /// 获取章节课件
        /// </summary>
        public List<resCourseListChildModel> GetzjResourcesCourseList(string tags)
        {
            int tagid = 0;
            int dir_name_id = 0;
            foreach (var dir in DirList)
            {
                foreach (var item in dir.dir_item)
                {
                    if (item.dir_name == dir_name)
                    {
                        dir_name_id = item.dir_name_id;
                    }
                }
            }
            //dir_name
            foreach (var tag in tagList)
            {
                if (tag.tag_name == tags)
                    tagid = tag.tag_id;
            }

            ReqCourseSourceListModel model = new ReqCourseSourceListModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = dir_name_id,
                tags = tagid,
                //id = 70,
                //tags = 18,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<List<resCourseListChildModel>>("/api/resources/getResourcesList", data, "获取资源列表");
            if (result.Item1)
            {
                zjkjList = result.Item2.data;
                return result.Item2.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }
        }

        public RelayCommand NextCommand
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {
                    if (string.IsNullOrEmpty(class_id))
                    {
                        ShowDialogView _tip = new ShowDialogView();
                        _tip.ShowDialogContent = "请选择课程";
                        _tip.ShowDialog();
                        return;
                    }

                    //selectChapterView.Hide();
                    SeletedResCount = 0;
                    is_selected_one_reference = false;
                    NextChapterView3 editGroupView = new NextChapterView3() { DataContext = this };
                    editGroupView.Owner = System.Windows.Application.Current.MainWindow;
                    editGroupView.ShowDialog();
                    if (editGroupView.ShowDialogResult)
                    {

                    }
                    else
                    {
                        if (ResourceCount <= 0)
                        {
                            BkName = string.Empty;
                            CurrentParentName = string.Empty;
                            CurrentCategoryName = string.Empty;
                        }
                    }
                }));
            }
        }
        public RelayCommand SaveCommand
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {
                    EditClassResource();
                }));
            }
        }

        public RelayCommand NextConfirmCommand
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {
                    //nextChapterView.Hide();
                    //planLessonView.UpdateImg(zjkjList1);

                    var _list_id = new HashSet<int>();
                    var _list_select = new List<resCourseListChildModel>();
                    foreach (var _item in EthanMyPackageList)
                    {
                        _list_id.Add(_item.id);
                    }
                    foreach (var _item in zjkjList2)
                    {
                        if (_list_id.Add(_item.id))
                        {
                            _list_select.Add(_item);
                            _item.type_token = 0;
                            EthanMyPackageList.Add(_item);
                        }
                    }
                    planLessonView.UpdateImg(_list_select);
                    SeletedResCount = 0;
                    ResourceViewSeletedResCount = 0;

                    nextChapterView.DialogResult = new bool?(true);
                }));
            }
        }

        public RelayCommand NextConfirmCommand2
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {
                    resourceView.Hide();
                    //planLessonView.UpdateImg2(CategoryCourseChildList1);
                    //planLessonView.UpdateImg4(MyCollectResourceList1);

                    var _list_id = new HashSet<int>();
                    var _list_select = new List<resCourseListChildModel>();
                    foreach (var _item in EthanMyPackageList)
                    {
                        _list_id.Add(_item.id);
                    }
                    foreach (var _item in zjkjList3)
                    {
                        if (_list_id.Add(_item.id))
                        {
                            var _model = new resCourseListChildModel
                            {
                                type_token = 1,
                                id = _item.id,
                                resources_name = _item.resources_name,
                                file = _item.file,
                                ext_name = _item.ext_name,
                                rotation_img_text = _item.rotation_img_text,
                                lyric_text = _item.lyric_text,
                            };
                            _list_select.Add(_model);
                            EthanMyPackageList.Add(_model);
                        }
                    }
                    planLessonView.UpdateImg(_list_select);
                }));
            }
        }

        public void Setattachment_ids(int type, string name)
        {
            string types = "";
            int id = 0;
            if (type == 0)
            {
                types = "t0";
                foreach (var item in zjkjList)
                {
                    if (name == item.resources_name)
                    {
                        KeZyImageUrl = item.cover;
                        id = item.id;
                        if (!zjkjList1.Contains(item))
                        {
                            zjkjList1.Add(item);
                            break;
                        }
                    }
                }
                //if (string.IsNullOrWhiteSpace(attachment_ids))
                //    attachment_ids += types + "_" + id.ToString();
                //else
                //    attachment_ids += "," + types + "_" + id.ToString();

            }
            if (type == 1)
            {
                types = "t1";
                foreach (var item in CategoryCourseChildList)
                {
                    if (name == item.resources_name)
                    {
                        ImageUrl = item.image;

                        id = item.id;
                        if (!CategoryCourseChildList1.Contains(item))
                        {
                            CategoryCourseChildList1.Add(item);
                            break;
                        }
                    }
                }
                //if (string.IsNullOrWhiteSpace(attachment_ids))
                //    attachment_ids += types + "_" + id.ToString();
                //else
                //    attachment_ids += "," + types + "_" + id.ToString();

            }
            if (type == 2)
            {
                types = "t2";
                //MySpaceListdata1 = MySpaceListdata;
                //foreach (var item in MySpaceListdata)
                //{
                //    if (CommonMethods.MySpaceSelectedId != null)
                //    {
                //        if (CommonMethods.MySpaceSelectedId.Contains(item.id.ToString()))
                //            id = item.id;
                //        if (id > 0)
                //        {
                //            if (string.IsNullOrWhiteSpace(attachment_ids))
                //                attachment_ids += types + "_" + id.ToString();
                //            else
                //                attachment_ids += "," + types + "_" + id.ToString();
                //            id = 0;
                //            MySpaceListdata1.Add(item);
                //        }
                //    }
                //}
            }
            if (type == 3)
            {
                foreach (var item in MyCollectResourceList0)
                {
                    if (name == item.resources_name)
                    {
                        MyImageUrl = item.image;
                        if (!MyCollectResourceList1.Contains(item))
                        {
                            MyCollectResourceList1.Add(item);
                            break;
                        }

                    }
                }
            }
        }

        public void RemoveSetattachment_ids(int type, string name)
        {
            int id = 0;
            if (type == 0)
            {

                foreach (var item in zjkjList1)
                {
                    if (name == item.resources_name)
                    {
                        zjkjList1.Remove(item);

                        //if (zjkjList1.Count <= 0)
                        break;
                    }
                }

            }
            if (type == 1)
            {
                foreach (var item in CategoryCourseChildList1)
                {
                    if (name == item.resources_name)
                    {
                        CategoryCourseChildList1.Remove(item);
                        //if (CategoryCourseChildList1.Count <= 0)
                        break;
                    }
                }


            }
            if (type == 2)
            {
                foreach (var item in MySpaceListdata)
                {
                    if (CommonMethods.MySpaceSelectedId != null)
                    {
                        if (CommonMethods.MySpaceSelectedId.Contains(item.id.ToString()))
                            id = item.id;
                        if (id > 0)
                        {

                            MySpaceListdata1.Remove(item);
                        }
                    }
                }
            }
            if (type == 3)
            {
                foreach (var item in MyCollectResourceList1)
                {
                    if (name == item.resources_name)
                    {

                        MyCollectResourceList1.Remove(item);
                        //if (MyCollectResourceList1.Count <= 0)
                        break;
                    }
                }
            }

        }

        private void spaceFileList()
        {
            ReqGroupSpaceFileModel pamrm = new ReqGroupSpaceFileModel
            {
                page = 1,
                limit = 10,
                token = HuiZeeducation.Instance.UserInfo.token,

            };
            string data = JsonHelper.ToJson(pamrm);

            var result = Request<ReqGroupFileListModel>("/api/user_space/spaceFileList", data, "空间文件列表");
            if (result.Item1)
            {
                MySpaceListdata = result.Item2.data.data;

            }
            else
            {

            }
        }

        public void SelectClass2(object obj)
        {
            var item = obj as TreeData;
            if (item.Children.Count == 0)
            {
                var list = HuiZeeducation.Instance.DownLoadClassData(item.id, "获取班级列表");
                if (list != null)
                {
                    Grades = new ObservableCollection<ClassModel>(list);
                }
            }

            foreach (var subitem in Grades)
            {
                subitem.parentName = item.DisplayName;
            }
        }

        public void ShowResDetail(string BkType, string BkId)
        {
            ShowResource2 showResource = new ShowResource2();
            showResource.DataContext = this;
            showResource.BkId = BkId;
            showResource.BkType = BkType;
            showResource.ShowDialog();
        }

        public List<ResResBkDetailModel> GetResDetailData()
        {
            ReqResBkDetailModel pamrm = new ReqResBkDetailModel
            {
                bid = ShowResDetailId,
                token = HuiZeeducation.Instance.UserInfo.token,
            };
            string data = JsonHelper.ToJson(pamrm);

            var result = Request<ResResBkDetailHeadModel>("/api/resources/beikelist", data, "查看备课包详情");
            if (result.Item1)
            {
                BkName = result.Item2.data.bclass.title;
                return result.Item2.data.resource_list;
            }
            else
            {
                ShowDialogView("获取备课资源详情失败!");
                return null;
            }
        }

        public void RemoveBkRes(string BkType, string BkId)
        {
            if (BkType == "0")
            {
                foreach (var item in zjkjList1)
                {
                    if (item.id.ToString() == BkId)
                    {
                        zjkjList1.Remove(item);
                        break;
                    }
                }
            }
            if (BkType == "1")
            {
                foreach (var item in CategoryCourseChildList1)
                {
                    if (item.id.ToString() == BkId)
                    {
                        CategoryCourseChildList1.Remove(item);
                        break;
                    }
                }
            }
            if (BkType == "3")
            {
                foreach (var item in MyCollectResourceList1)
                {
                    if (item.id.ToString() == BkId)
                    {
                        MyCollectResourceList1.Remove(item);
                        break;
                    }
                }
            }


        }

        public resCourseListChildModel GetResourceKcZy(string BkId)
        {
            resCourseListChildModel returnObj = new resCourseListChildModel();
            foreach (var item in zjkjList1)
            {
                if (item.id.ToString() == BkId)
                {
                    returnObj = item;
                }
            }
            return returnObj;

        }
        public resBKCategoryCourseChildModel GetResourceCenter(string BkId)
        {
            resBKCategoryCourseChildModel returnObj = new resBKCategoryCourseChildModel();

            foreach (var item in CategoryCourseChildList1)
            {
                if (item.id.ToString() == BkId)
                {
                    returnObj = item; ;
                }
            }


            return returnObj;

        }
        public ReqGroupFileListDetialModel GetResourceMySpace(string BkId)
        {
            ReqGroupFileListDetialModel returnObj = new ReqGroupFileListDetialModel();


            foreach (var item in MySpaceListdata1)
            {
                if (item.id.ToString() == BkId)
                {
                    returnObj = item; ;
                }
            }

            return returnObj;
        }
        public resBKCategoryCourseChildModel GetMyCllection(string BkId)
        {
            resBKCategoryCourseChildModel returnObj = new resBKCategoryCourseChildModel();


            foreach (var item in MyCollectResourceList1)
            {
                if (item.id.ToString() == BkId)
                {
                    returnObj = item; ;
                }
            }

            return returnObj;
        }
    }
}
